<?php
include('admin.php');
set_page_info('id', 'pages');

if (ACTION == 'new') {
	if (isset($_POST['submit'])) {
		$c = get_post_data();
		
		/***TITLE AND CONTENT***/
		if (empty($c['title']) || empty($c['content'])) error('Please enter a title and some content.');
		
		/***SLUG***/
		if (empty($c['slug'])) $c['slug'] = get_slug($c['title']);
		$amount = (int)mysql_result($mysql->query('SELECT COUNT(*) FROM `'.PAGES.'` WHERE `page_title_slug` = "'.escape($c['slug']).'"'), 0);
		if ($amount > 0) $c['slug'] = $c['slug'].'-'.$amount;
		unset($amount);
		
		/***CUSTOM FIELDS***/
		$c['custom_fields'] = array();
		if (!empty($c['custom_key']) && !empty($c['custom_value']) && count($c['custom_key']) > 0 && count($c['custom_value']) > 0 && count($c['custom_key']) === count($c['custom_value'])) {
			foreach($c['custom_key'] as $key => $value) {
				$c['custom_fields'][$value] = $c['custom_value'][$key];
			}
		}
		
		/***STATUS***/
		$c['status'] = (int)$c['status'];
		if (!empty($c['status'])) $c['status'] = 1;
		else $c['status'] = 0;
		
		$c['content'] = clean($_POST['content'], true);
		$result = $mysql->insert(PAGES, array(
			'page_title' => $c['title'],
			'page_title_slug' => $c['slug'],
			'page_date' => get_date($dateFormats['mysql']),
			'page_content' => $c['content'],
			'news' => $c['news'],
			'page_status' => $c['status'],
			'titletag' => $c['titletag'],
			'metatag' => $c['metatag'],
			'page_custom_fields' => (count($c['custom_fields']) > 0) ? serialize($c['custom_fields']) : ''
		));
		if ($result === true) header('Location: pages.php#pages-'.$mysql->lastInsertId);
		else error('There was an error saving the page.');
	} else {
		set_page_info('title', 'New Page');
		get_header();
?>
		<h2>New Page</h2>
		<form action="pages.php?action=new" method="post">
			<p><label for="title">Title</label>
			<input type="text" name="title" id="title" class="slug" /></p>
			
			<p><label for="slug">Slug</label>
			<input type="text" name="slug" id="slug" class="slughere" /></p>
			
            
            <p><label for="slug">News</label>
            <select name="news" id="news" class="news">
            <option value="0">Latest news</option>
            <?php 
			$sql_select_query = "select * from aa_posts ORDER BY post_category ASC";
		    $mysql_result = mysql_query($sql_select_query);
		    
			$newsstart = 0;
			$tipstart = 0;
			$uncatstart = 0;
			
			while($datarow = mysql_fetch_array($mysql_result))
		    {
				if($datarow['post_category'] == 1 && $uncatstart == 0)
				{
					print "<option value=\"\">Uncategorized</option>";
					$uncatstart = 1;
				}
				if($datarow['post_category'] == 2 && $tipstart == 0)
				{
					print "<option value=\"\">Tips & Tricks</option>";
					$tipstart = 1;
				}
				if($datarow['post_category'] == 3 && $newsstart == 0)
				{
					print "<option value=\"\">News</option>";
					$newsstart = 1;
				}
				
				
				
				if($datarow['post_category'] == 1)
				{
					print "<option value=\"$datarow[post_id]\">&nbsp;&nbsp; $datarow[post_title]</option>";
				}
				elseif($datarow['post_category'] == 2)
				{
					print "<option value=\"$datarow[post_id]\">&nbsp;&nbsp; $datarow[post_title]</option>";
				}
				elseif($datarow['post_category'] == 3)
				{
					print "<option value=\"$datarow[post_id]\">&nbsp;&nbsp; $datarow[post_title]</option>";
				}
			}
			
			?>
            
            </select>
            
			<p><label for="content">Content</label>
			<textarea name="content" id="content" class="large" rows="6" cols="60"></textarea>
             <script type="text/javascript">
				var oEdit1 = new InnovaEditor("oEdit1");
				oEdit1.REPLACE("content");
			</script>
            </p>
			
             <p>
            <label for "titletag">Title Tag</label>
            <input type="text" name="titletag" id="titletag" value=""   />
            </p>
            
             <p>
            <label for "metatag">Meta Tag</label>
            <input type="text" name="metatag" id="metatag" value=""   />
            </p>
            
			<div class="paragraph">
				<label>Custom Fields</label>
				<div id="custom_fields">
					<div id="fields"></div>
					<div class="field">
						<div class="left">
							<input type="text" id="js_custom_key" />
							<input type="submit" id="js_save_custom_field" class="button" value="Save" />
						</div>
						
						<textarea id="js_custom_value" rows="3" cols="30"></textarea>
					</div>
				</div>
			</div>
			
			<p>
				<label>Status</label>
				<input type="radio" name="status" id="status[0]" class="radio" value="1" checked="checked" /> <label for="status[0]" class="radiolabel">Published</label>
				<input type="radio" name="status" id="status[1]" class="radio" value="0" /> <label for="status[1]" class="radiolabel">Draft</label>
			</p>
			
			<p><input type="submit" name="submit" class="button" value="Save" /></p>
		</form>
<?php
		get_footer();
	}
} elseif (ACTION == 'edit') {
	$id = (int)get_global($_GET, 'id');
	$page = get_single_item(array(
		'table' => PAGES,
		'class' => 'page',
		'where' => '`page_id` = "'.$id.'"'
	));
	if (empty($page)) error('That is not a valid page.');
	
	if (isset($_POST['submit'])) {
		$c = get_post_data();
		
		/***TITLE AND CONTENT***/
		//if (empty($c['title'])) $c['title'] = $post->title;
		//if (empty($c['content'])) $c['content'] = $post->content;
		$c['content'] = clean($_POST['content'], true);
		
		/***SLUG***/
		if (empty($c['slug'])) $c['slug'] = get_slug($c['title']);
		$amount = (int)mysql_result($mysql->query('SELECT COUNT(*) FROM `'.PAGES.'` WHERE `page_title_slug` = "'.escape($c['slug']).'" AND `page_id` != "'.$page->get_id().'"'), 0);
		if ($amount > 0) $c['slug'] = $c['slug'].'-'.$amount;
		unset($amount);
		
		/***CUSTOM FIELDS***/
	
		$c['custom_fields'] = array();
		if (!empty($c['custom_key']) && !empty($c['custom_value']) && count($c['custom_key']) > 0 && count($c['custom_value']) > 0 && count($c['custom_key']) === count($c['custom_value'])) {
			foreach($c['custom_key'] as $key => $value) {
				$c['custom_fields'][$value] = $c['custom_value'][$key];
			}
		}
	
		
		/***STATUS***/
		$c['status'] = (int)$c['status'];
		if (!empty($c['status'])) $c['status'] = 1;
		else $c['status'] = 0;
		
		$result = $mysql->update(PAGES, array(
			'page_title' => $c['title'],
			'page_title_slug' => $c['slug'],
			'page_content' => $c['content'],
			'page_status' => $c['status'],
			'news' => $c['news'],
			'titletag' => $c['titletag'],
			'metatag' => $c['metatag'],
			'page_custom_fields' => (count($c['custom_fields']) > 0) ? serialize($c['custom_fields']) : ''
		), '`page_id` = "'.$page->get_id().'"');
		if ($result === true) header('Location: pages.php#pages-'.$page->get_id());
		else error('There was an error saving the page.');
	} else {
		set_page_info('Editing Page: '.$page->get_title());
		get_header();
?>
		<h2>Editing Page: <?php $page->the_title(); ?></h2>
		<form action="pages.php?action=edit&amp;id=<?php $page->the_id(); ?>" method="post">
			<p><label for="title">Title</label>
			<input type="text" name="title" id="title" class="slug" value="<?php $page->the_title(); ?>" /></p>
			
			<p><label for="slug">Slug</label>
			<input type="text" name="slug" id="slug" class="slughere" value="<?php $page->the_slug(); ?>" /></p>
            
            <p><label for="slug">News</label>
            <select name="news" id="news" class="news">
            <option value="<?php $page->the_news(); ?>">No changes</option>
            <option value="0">Latest news</option>
            <?php 
			$sql_select_query = "select * from aa_posts where post_category = '3' ";
		    $mysql_result = mysql_query($sql_select_query);
		    while($datarow = mysql_fetch_array($mysql_result))
		    {
				print "<option value=\"$datarow[post_id]\">$datarow[post_title]</option>";
			}
			
			?>
            
            </select>
			
			
			<p><label for="content_lbl">Content</label>
			<textarea name="content" id="content" rows="6" cols="60">
			<?php
			
			 echo $page->the_content(false);
			
			?>
            
            
            </textarea>
            </p>
             <script type="text/javascript">
				var oEdit1 = new InnovaEditor("oEdit1");
				oEdit1.cmdAssetManager="modalDialogShow('/admin/editor/assetmanager/assetmanager.php',640,445);";
				oEdit1.REPLACE("content");
			</script>
            
			 <p>
            <label for "titletag">Title Tag</label>
            <input type="text" name="titletag" id="titletag" value="<?php print $page->get_titletag(); ?>" />
            </p>
            
             <p>
            <label for "metatag">Meta Tag</label>
            <input type="text" name="metatag" id="metatag" value="<?php print $page->get_metatag(); ?>" />
            </p>
           
			<div class="paragraph">
				<label>Custom Fields</label>
				<div id="custom_fields">
					<div id="fields">
<?php
if (!empty($page->custom_fields)) {
	$custom_fields = unserialize($page->custom_fields);
	foreach($custom_fields as $key => $value) {
?>
						<div class="field">
							<div class="left">
								<input type="text" name="custom_key[]" class="custom_key" value="<?php echo $key; ?>" />
								<input type="submit" class="button custom_delete" value="Delete" />
							</div>
							<textarea name="custom_value[]" class="custom_value" rows="3" cols="30"><?php echo $value; ?></textarea>
						</div>
<?php
	}
}
?>
					</div>
					<div class="field">
						<div class="left">
							<input type="text" id="js_custom_key" />
							<input type="submit" id="js_save_custom_field" class="button" value="Save" />
						</div>
						
						<textarea id="js_custom_value" rows="3" cols="30"></textarea>
					</div>
				</div>
			</div>
			
			<p>
				<label>Status</label>
				<input type="radio" name="status" id="status[0]" class="radio" value="1"<?php if ($page->get_status()) echo ' checked="checked"'; ?> /> <label for="status[0]" class="radiolabel">Published</label>
				<input type="radio" name="status" id="status[1]" class="radio" value="0"<?php if (!$page->get_status()) echo ' checked="checked"'; ?> /> <label for="status[1]" class="radiolabel">Draft</label>
			</p>
			
			<p><input type="submit" name="submit" class="button" value="Save" /></p>
		</form>
<?php
		get_footer();
	}
} elseif (ACTION == 'delete') {
	$id = (int)get_global($_GET, 'id');
	$result = delete_page($id);
	
	if ($result === true) header('Location: pages.php');
	else error('There was an error deleting the page.');
} else {
	$approved = get_pages(array('paginationVar'=>'approved'), true);
	$unapproved = get_pages(array(
		'where' => '`page_status` = "0"',
		'pageinationVar'=>'unapproved'
	));
	set_page_info('title', 'Manage Pages');
	get_header();
	
	if ($approved->total > 0) {
?>
		<h2>Approved Pages <small>(<?php echo $approved->total; ?>)</small> <span>(<a href="pages.php?action=new">add one?</a>)</span></h2>
		<form action="mass-delete.php?type=pages" method="post">
			<table  width="100%">
				<thead>
					<tr>
						<th><input type="checkbox" /></th>
						<th>Title</th>
						<th>Date</th>
						<th>&nbsp;</th>
					</tr>
				</thead>
				<tfoot>
					<tr>
						<td colspan="4">
							<input type="submit" name="submit" class="button" value="Delete" />
							Page: <?php echo $approved->pagination['page']; ?><?php if ($approved->needsPagination()) { echo ' &mdash; '; $approved->pagination(); } ?>
						</td>
					</tr>
				</tfoot>
				<tbody>
<?php foreach($approved->items as $page) { ?>
					<tr<?php $page->the_row_info(); ?>>
						<td class="checkbox"><input type="checkbox" name="mass_delete[]" value="<?php $page->the_id(); ?>" /></td>
						<td><?php $page->the_title(); ?></td>
						<td><?php $page->the_date(); ?></td>
						<td class="options"><?php $page->the_options(); ?></td>
					</tr>
<?php } ?>
				</tbody>
			</table>
		</form>
<?php
	}
	
	if ($unapproved->total > 0) {
?>
		<h2>Unapproved Posts <small>(<?php echo $unapproved->total; ?>)</small> <span>(<a href="pages.php?action=new">add one?</a>)</span></h2>
		<form action="mass-delete.php?type=pages" method="post">
			<table  width="100%">
				<thead>
					<tr>
						<th><input type="checkbox" /></th>
						<th>Title</th>
						<th>Date</th>
						<th>&nbsp;</th>
					</tr>
				</thead>
				<tfoot>
					<tr>
						<td colspan="4">
							<input type="submit" name="submit" class="button" value="Delete" />
							Page: <?php echo $unapproved->pagination['page']; ?><?php if ($unapproved->needsPagination()) { echo ' &mdash; '; $unapproved->pagination(); } ?>
						</td>
					</tr>
				</tfoot>
				<tbody>
<?php foreach($unapproved->items as $page) { ?>
					<tr<?php $page->the_row_info(); ?>>
						<td class="checkbox"><input type="checkbox" name="mass_delete[]" value="<?php $page->the_id(); ?>" /></td>
						<td><?php $page->the_title(); ?></td>
						<td><?php $page->the_date(); ?></td>
						<td class="options"><?php $page->the_options(); ?></td>
					</tr>
<?php } ?>
				</tbody>
			</table>
		</form>
<?php
	}
	
	if ($approved->total === 0 && $unapproved->total === 0) {
?>
		<h2>No Pages<span>(<a href="pages.php?action=new">add one?</a>)</span></h2>
		<form action="mass-delete.php?type=pages" method="post">
			<table  width="100%">
				<thead>
					<tr>
						<th><input type="checkbox" /></th>
						<th>Title</th>
						<th>Date</th>
						<th>&nbsp;</th>
					</tr>
				</thead>
				<tfoot>
					<tr>
						<td colspan="4">
							<input type="submit" name="submit" class="button" value="Delete" />
							Page: <?php echo $approved->pagination['page']; ?>
						</td>
					</tr>
				</tfoot>
				<tbody>
					<tr>
						<td colspan="4">No pages found.</td>
					</tr>
				</tbody>
			</table>
		</form>
<?php
	}
	get_footer();
}
?>